<?php
/**
 * Message
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     yuki.li <yuki_li@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

class Message extends Zend_Db_Table
{	
	/*
	$data		//an associative array of columns to change and new values to assign to these columns
	$where		//a WHERE clause
	*/

	public $_db = null;
	public $_filter_list =array();
	public $_raw_task_list = array();
	public $_sup_task_list = array();

	
	public function __construct () {
		$this->_db = Zend_Registry :: get('db_adapter');
		$this->_name = 'tt_messages';
		$this->_primary = 'id';
		parent::_setup();
	}
	
	public function setFilter ($filter_list = array()) {
		$this->_filter_list = $filter_list;
	}
	
	public function getFilter ($prefix = '') {
		if (sizeof($this->_filter_list) <= 0) return '';
		$filter_arr = array();
		foreach ($this->_filter_list as $k => $v) {
			if ($v <= 0) continue;
			array_push($filter_arr, $k.' = '.$v);
		}
		if (sizeof($filter_arr) > 0) {
			return $prefix.implode(' and ', $filter_arr);
		} else {
			return '';
		}
	}

	/**
	 * custom update
	 *
	 * @param array $data
	 * @param integer $id
	 * @return mixed
	 * @throws Zend_Db_Exception
	 */
    public function update (array $data, $id)
    {
        $where = $this->_db->quoteInto('id = ?', $id);
        return parent::update($data, $where);
    }

	public function deleMessage ($id) {
		/*
		UPDATE tt_messages
		SET flag = "yes"
		WHERE id = $id
		*/

		//when flag is yes, this message was deleted
		$data = array ('flag' => 'yes',);
		print_r($data);
		$this->update($data,$id);
	}
	
	public function getCols () {
		return $this->_cols;
	}
	
	public function getTotal () {
		$authNamespace = Zend_Auth::getInstance()->getIdentity();
		$sql = "SELECT count(*)
		        FROM tt_messages m
				WHERE m.userid = ".$authNamespace['userId']."				  
				  AND m.status <> -1  
			   ";
		return $this->_db->fetchOne($sql);
	}
	
	public function getMessagesList($params)
	{
		$developer_id =$params['userId'];
		$params['frNum'] >0? $fromItem=$params['frNum']:$fromItem =0;
		$params['itemNum'] >0? $page_item=$params['itemNum']:$page_item =0;
		if($params['listType'] =='s'){
			$userIdField = "userid";
		}else{
			$userIdField = "toid";
		}
			   
			   $sql = "SELECT m.id,
		               m.msgid,
		               m.taskid,
		               m.content,
		               m.userid,
		               u.name,
		               m.adddate
		        FROM tt_messages m, tt_user u
				WHERE m.".$userIdField." = ".$developer_id."				  
				  AND u.id = m.userid
				  AND m.status <> -1  
				ORDER BY m.id desc
				LIMIT $fromItem, $page_item
			   ";   
	   
		return $this->_db->fetchAll($sql);
	}
	
	/*get the unread message count for a user*/
	public function getUnreadMsgCount($userId){
		$sql = "SELECT count(*) as num
		        FROM tt_messages WHERE toId = ".$userId."
				  AND status =0";
		return $this->_db->fetchOne($sql);			
	}
}
?>